// 代理地址 [head 代理头] [local 本地] [network 网络]
const onlinePath = ''
const localServer = 'http://51.24.1.112:10000'
const proxyPathMap = {
  // 业务接口-陈智睿
  apiWorkCZR: {
    local: localServer + '/popularfeelings',
    network: '/popularfeelings'
  },
  // 登录接口-陈智睿
  loginAuth: {
    local: localServer + '/popularfeelings',
    network: '/popularfeelings'
  }
}

/**
 * 获取代理地址
 * @param {string} key
 * @returns
 */
export function getProxyPath(key) {
  const path = proxyPathMap[key]
  if (!path) {
    console.warn('未找到对应的地址配置')
    return ''
  }
  const env = process.env.NODE_ENV
  if (env === 'development') {
    return `/${key}`
  } else {
    return path.network
  }
}

/**根据不同环境创建代理 */
export function createProxy(isNetwork) {
  if (isNetwork) {
    console.log('\x1B[33m%s\x1b[0m:', '[注意: 线上开发环境连接的是正式环境的接口地址， 请谨慎操作!]')
  }
  const proxy = {}
  for (const key in proxyPathMap) {
    const item = proxyPathMap[key]
    const target = isNetwork ? onlinePath : item.local
    proxy[`/${key}`] = {
      target,
      changeOrigin: true,
      rewrite: (path) => path.replace(RegExp(`^/${key}`), '')
    }
  }
  return proxy
}
